package org.groupg.demo.sshd;

import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.apache.sshd.server.auth.password.PasswordAuthenticator;
import org.apache.sshd.server.session.ServerSession;

import java.util.Arrays;

public class SftpServerExample {
    public static void main(String[] args) throws Exception {
        SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(2222);

        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider());

        sshd.setPasswordAuthenticator(new PasswordAuthenticator() {
            @Override
            public boolean authenticate(String username, String password, ServerSession session) {
                return "admin".equals(username) && "password".equals(password);
            }
        });

        // 添加SFTP子系统
        sshd.setSubsystemFactories(Arrays.asList(new SftpSubsystemFactory()));

        sshd.start();
        System.out.println("SFTP server started on port 2222");

        Thread.sleep(Long.MAX_VALUE);
    }
}
